export const TOAST = (title) => {
  uni.showToast({title: title, icon: 'none'})
}

export const CHECK_PHONE = (phone) => {
  var phoneReg = /^\d{11}$/;
  if (phoneReg.test(phone)) {
    return true;
  } else {
    return false;
  }
}

export const CHECK_EMAIL = (email) => {
  var reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
  if (reg.test(email)) {
    return true;
  } else {
    return false;
  }
}


/**
 * 四则运算
 *
 * @param x
 * @param y
 * @param op 操作符，0：加；1：减；2：乘；3：除
 * @param acc 保留小数位个数，进行四舍五入
 */
export const execute = (x, y, op, acc) => {
  var xx = Number(x == undefined ? 0 : x);
  var yy = Number(y == undefined ? 0 : y);

  //
  var a = science(xx);
  var b = science(yy);

  var na = a.r1;
  var nb = b.r1;

  var ta = a.r2;
  var tb = b.r2;
  var maxt = Math.max(ta, tb);

  //精度值处理
  var result = 0;
  switch (parseInt(op, 10)) {
    case 0: //加
      result = (xx * maxt + yy * maxt) / maxt;
      break;
    case 1: //减
      result = (xx * maxt - yy * maxt) / maxt;
      break;
    case 2:  // 乘
      result = na * nb / (ta * tb);
      break;
    case 3: // 除
      result = na / nb * (tb / ta);
    default:
  }
	if (result < 0.1) {
		acc = 4
	} else {
		acc = 2
	}
  //小数位数处理
  if (acc) {
    return Number(Number(result).toFixed(parseInt(acc)));
  } else {
    return Number(result);
  }
}

/**
 * 将数值升级(10的X的次方)到整数
 */
export const science = (num) => {
  var re = {
    r1: 0, //数字去掉小数点后的值，也就是 r1*r2 的结果
    r2: 1 //小数部分，10的长度次幂
  };
  if (isInteger(num)) { //整数直接返回
    re.r1 = num;
    return re;
  }
  var snum = scienceNum(num + ""); //处理0.123e-10类似问题
  var dotPos = snum.indexOf("."); //小数点位置
  var len = snum.substr(dotPos + 1).length; //小数点长度
  re.r2 = Math.pow(10, len);
  re.r1 = parseInt(snum.replace(".", ""));
  return re;
}

/**
 * 将数值转为字符串
 *
 * 通过移动小数点  扩大倍数或缩小倍数(解决出现e+、e-的问题)
 *
 * JavaScript在以下情景会自动将数值转换为科学计数法：
 *   1）小数点前的数字多于21位。
 *   2）小数点后的零多于5个
 */
export const scienceNum = (value) => {
  if (!value) {
    return value;
  }
  if (typeof value === 'number') {
    value = value + ""
  }
  ;
  let eIndex = value.indexOf('E');
  if (eIndex == -1) {
    eIndex = value.indexOf('e')
  }
  ;
  if (eIndex != -1) {
    let doubleStr = value.substring(0, eIndex); //e前面的值
    let eStr = parseInt(value.substring(eIndex + 1, value.length)); //e后面的值
    let doubleStrArr = doubleStr.split('.');
    let doubleStr1 = doubleStrArr[0] || "";
    let doubleStr2 = doubleStrArr[1] || "";

    if (eStr < 0) { //e- 很小的数
      let str1Len = doubleStr1.length;
      let eStrs = Math.abs(eStr);
      if (str1Len > eStrs) {
        let nums = doubleStr1.substring(0, eStrs);
        let nume = doubleStr1.substring(eStrs, str1Len);
        doubleStr = nums + "." + nume + nume;
      } else if (str1Len < eStrs) {
        let indexNum = eStrs - str1Len;
        let str = _makeZero(indexNum); //用0补齐
        doubleStr = '0.' + str + doubleStr1 + doubleStr2;
      } else {
        doubleStr = '0.' + doubleStr1 + doubleStr2;
      }
    } else {  //e+ 很大的数
      let str2Len = doubleStr2.length;
      if (str2Len > eStr) {
        let _nums = doubleStr2.substring(0, eStr);
        let _nume = doubleStr2.substring(eStr, str2Len);
        doubleStr = doubleStr1 + _nums + '.' + _nume;
      } else if (str2Len < eStr) {
        let _indexNum = eStr - str2Len;
        let _str = _makeZero(_indexNum); //用0补齐
        doubleStr = doubleStr1 + doubleStr2 + _str;
      } else {
        doubleStr = doubleStr1 + doubleStr2;
      }
    }
    value = doubleStr;
  }
  return value;
}

//生成num个0的字符串
export const _makeZero = (num) => {
  var str = '';
  for (var i = 0; i < num; i++) {
    str += '0';
  }
  return str;
}

/**
 * 判断是否为整数，字符整数也返回true
 *
 * @param num
 * @returns
 */
export const isInteger = (num) => {
  return Math.floor(num) === Number(num);
}

//加法运算
export const add = (x, y, acc) => {
  return execute(x, y, 0, acc);
}

//减法运算
export const subtract = (x, y, acc) => {
  return execute(x, y, 1, acc);
}

//乘法运算
export const multiply = (x, y, acc) => {
  return execute(x, y, 2, acc);
}

//除法运算
export const divide = (x, y, acc) => {
  return execute(x, y, 3, acc);
}

export const CHECK_TOKEN = () => {
  let user = uni.getStorageSync('zbz_user')
  if (user && user.token) {
    return true
  } else {
    return false
  }
}

export const SWIPER_JUMP = (type) => {
  let url = ''
  let jumpType = 'navigateTo'
  switch (Number(type)) {
    case 1:
      url = '/pages/user/wallet'
      break
    case 2:
      url = '/pages/pince/index'
      jumpType = 'switchTab'
      break
    case 3:
      url = '/pages/user/adviser?title=1'
      break
    case 4:
      url = '/pages/user/achievement'
      break
    case 5:
      url = '/pages/curriculum/index'
      break
  }
  if (!url) {
    return
  }
  if (jumpType == 'switchTab') {
    uni.switchTab({
      url: url
    })
  } else {
    uni.navigateTo({
      url: url
    })
  }

}

export const getCurrentFullPath = () => {
  var backUrl = ''
  var pages = getCurrentPages()
  let {route,options} = pages[pages.length - 1]
  if (options && Object.keys(options).length) {
    let paramsStr = '?'
    for(var n in options) {
      paramsStr += `${n}=${options[n]}&`
    }
    paramsStr = paramsStr.substr(0, paramsStr.length - 1)
    backUrl = route + paramsStr
  } else {
    backUrl = route
  }
  return backUrl
}
